<?php

use Biz\model\wiki\WikiRankingBrand;
use Biz\model\wiki\WikiRankingHot;
use Biz\model\wiki\WikiRankingPublicPraise;
use Biz\service\wiki\Wiki as sWiki;
use Biz\service\wikiRanking\Ranking;
use Biz\service\brand\Brand;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\service\User;
use Doraemon\tools\BlockType;

/**
 * 好物频道榜单信息
 * Class RankingAction
 */
class RankingAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    public function main()
    {
        $rankingId = $this->getRequest()->getQuery('ranking_id', 0);
        if (!$rankingId) {
            throw new \Exception('参数错误');
        }
        $data = $this->getRanking($rankingId);
        $this->setSuccess($data);
    }

    private function getRanking($rankingId)
    {
        $result = [
            'hot' => [
                'type' => BlockType::TYPE_WIKI,
                'list' => [],
                'link' => Ranking::getRankingUrl($rankingId, 1),
                'statSign' => basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_RANKING, [
                    basicStatSign::EXTEND_KEY_OBJ_ID => $rankingId, basicStatSign::EXTEND_KEY_OBJ_TYPE => 'hot'
                ], ['ranking_id' => $rankingId, 'ranking_type' => 'hot'])
            ],
            'public_praise' => [
                'type' => BlockType::TYPE_WIKI,
                'list' => [],
                'link' => Ranking::getRankingUrl($rankingId, 2),
                'statSign' => basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_RANKING, [
                    basicStatSign::EXTEND_KEY_OBJ_ID => $rankingId, basicStatSign::EXTEND_KEY_OBJ_TYPE => 'public_praise'
                ], ['ranking_id' => $rankingId, 'ranking_type' => 'public_praise'])
            ],
            'brand' => [
                'type' => BlockType::TYPE_BRAND,
                'list' => [],
                'link' => Ranking::getRankingUrl($rankingId, 3),
                'statSign' => basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_RANKING, [
                    basicStatSign::EXTEND_KEY_OBJ_ID => $rankingId, basicStatSign::EXTEND_KEY_OBJ_TYPE => 'brand'
                ], ['ranking_id' => $rankingId, 'ranking_type' => 'brand'])
            ]
        ];
        $rankingDay = Ranking::getRankingDay();
        $hotRankList = WikiRankingHot::getShowTop($rankingId, $rankingDay);
        $hotBasicIds = array_column($hotRankList, 'basic_id');
        $hotBasicPicHash =  Ranking::getBasicPicHash($hotRankList);
        $publicPraiseRankList = WikiRankingPublicPraise::getShowTop($rankingId, $rankingDay);
        $publicPraiseBasicIds = array_column($publicPraiseRankList, 'basic_id');
        $publicBasicPicHash =  Ranking::getBasicPicHash($publicPraiseRankList);
        $basicIds = array_merge($hotBasicIds, $publicPraiseBasicIds);
        $basicIds = array_unique($basicIds);
        $basicPicHash = $hotBasicPicHash + $publicBasicPicHash;
        $brandRankList = WikiRankingBrand::getShowTop($rankingId, $rankingDay);
        $brandIds = array_column($brandRankList, 'brand_id');
        $basicList = $basicIds ? sWiki::getWikiListByBasicIds($basicIds, $this->uid) : [];
        $basicList = Ranking::HandleBasicPic($basicList , $basicPicHash);
        $brandList = $brandIds ? Brand::getBrandListByIds4App($brandIds) : [];

        foreach ($hotRankList as $row) {
            $basicInfo = isset($basicList[$row['basic_id']]) ? $basicList[$row['basic_id']] : [];
            if ($basicInfo) {
                $result['hot']['list'][] = [
                    'id' => $row['basic_id'],
                    'top_num' => $row['top_num'],
                    'cover_img' => $basicInfo['cover_img'],
                ];
            }
        }
        if ($result['hot']['list']) {
            $hotRankList = array_slice($result['hot']['list'], 0, 3);
            $topNumSort = array_column($hotRankList, 'top_num');
            array_multisort($topNumSort, SORT_ASC, $hotRankList);
            $result['hot']['list'] = $hotRankList;
        } else {
            unset($result['hot']);
        }

        foreach ($publicPraiseRankList as $row) {
            $basicInfo = isset($basicList[$row['basic_id']]) ? $basicList[$row['basic_id']] : [];
            if ($basicInfo) {
                $result['public_praise']['list'][] = [
                    'id' => $row['basic_id'],
                    'top_num' => $row['top_num'],
                    'cover_img' => $basicInfo['cover_img'],
                ];
            }
        }
        if ($result['public_praise']['list']) {
            $publicPraiseRankList = array_slice($result['public_praise']['list'], 0, 3);
            $topNumSort = array_column($publicPraiseRankList, 'top_num');
            array_multisort($topNumSort, SORT_ASC, $publicPraiseRankList);
            $result['public_praise']['list'] = $publicPraiseRankList;
        } else {
            unset($result['public_praise']);
        }

        foreach ($brandRankList as $row) {
            $brandInfo = isset($brandList[$row['brand_id']]) ? $brandList[$row['brand_id']] : [];
            if ($brandInfo) {
                $brandUid = $brandInfo['uid'];
                $coverImg = $brandInfo['logo'];
                if (empty($logo) && $brandUid) {
                    $userInfo = User::getBasicUserInfo($brandUid);
                    $coverImg = $userInfo ? $userInfo['avatar'] : '';
                }
                $result['brand']['list'][] = [
                    'id' => $row['brand_id'],
                    'top_num' => $row['top_num'],
                    'cover_img' => $coverImg,
                ];
            }
        }
        if ($result['brand']['list']) {
            $brandRankList = array_slice($result['brand']['list'], 0, 3);
            $topNumSort = array_column($brandRankList, 'top_num');
            array_multisort($topNumSort, SORT_ASC, $brandRankList);
            $result['brand']['list'] = $brandRankList;
        } else {
            unset($result['brand']);
        }
        if ($result) {
            $result['link'] = Ranking::getRankingUrl($rankingId, 1);
        }
        return $result;
    }
}